package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.epg.EpgHotWordMapper;
import com.ztgf.shopping.models.bo.epg.EpgHotWordBO;
import com.ztgf.shopping.models.bo.epg.EpgHotWordSaveBO;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.dto.epg.EpgHotWordDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.epg.IEpgHotWordService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

/**
 * author: chenyu.wang
 * date: 2020-05-18 11:16
 * Description: 热词表控制器
 */
@DubboService
public class EpgHotWordServiceImpl extends ServiceImpl<EpgHotWordMapper, EpgHotWordDTO> implements IEpgHotWordService {

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private EpgHotWordMapper epgHotWordMapper;

    /**
     * 添加热词次数
     *
     * @param name
     * @return
     */
    //@Async
    @Override
    public void updateHotWordOnceByName(String name) {
        EpgHotWordDTO epgHotWordDTO = getByName(name);
        if(null == epgHotWordDTO){
            return;
        }
        epgHotWordDTO.setSearchNum(epgHotWordDTO.getSearchNum() + 1);
        updateById(epgHotWordDTO);
    }

    /**
     * 根据热词名字来查找热词
     * @param hotWordName
     * @return
     */
    @Override
    public EpgHotWordDTO getByName(String hotWordName){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("hot_word", hotWordName);
        return selectOne(wrapper);
    }


    @Override
    public Result add(EpgHotWordSaveBO hotWordSaveBO) {
        EpgHotWordDTO tempHotWordDTO = getByName(hotWordSaveBO.getHotWord());
        if(null != tempHotWordDTO){//不能添加相同的热词
            return new Result(CodeEnum.FAIL_100032);
        }
        int i = selectCount(new EntityWrapper<>());
        if(i >= 5){
            return new Result(CodeEnum.FAIL_100031);
        }
        EpgHotWordDTO epgHotWordDTO = new EpgHotWordDTO();
        BeanUtils.copyProperties(hotWordSaveBO, epgHotWordDTO);
        epgHotWordDTO.setId(ymlConfig.getPkId());
        this.insert(epgHotWordDTO);
        return new Result();
    }

    /**
     * 根据需要的信息进行查询热词 有页码
     *
     * @param epgHotWordBO
     * @return
     */
    @Override
    public Page<EpgHotWordDTO> getBySelectHotInfoPage(EpgHotWordBO epgHotWordBO) {
        epgHotWordBO = addMustData(epgHotWordBO);
        Wrapper<EpgHotWordDTO> wrapper = getWrapperByBOInfo(epgHotWordBO);
        return selectPage(new Page<>(epgHotWordBO.getPageNo(), epgHotWordBO.getPageSize()), wrapper);
    }

    /**
     * 添加必要数据
     *
     * @param epgHotWordBO
     * @return
     */
    private EpgHotWordBO addMustData(EpgHotWordBO epgHotWordBO) {
        epgHotWordBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgHotWordBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
        epgHotWordBO.setSort(ShoppingCodeEnum.SORT_DESC.getCode());
        return epgHotWordBO;
    }

    /**
     * 根据需要的信息进行查询热词 无页码
     *
     * @param epgHotWordBO
     * @return
     */
    @Override
    public List<EpgHotWordDTO> getBySelectHotInfoNoPage(EpgHotWordBO epgHotWordBO) {
        epgHotWordBO = addMustData(epgHotWordBO);
        Wrapper<EpgHotWordDTO> wrapper = getWrapperByBOInfo(epgHotWordBO);
        if (epgHotWordBO.getPageNo() == 0 && epgHotWordBO.getPageSize() == 0)
            return epgHotWordMapper.selectPage(new Page<>(0, ShoppingConstant.HOT_WORD_NUM), wrapper);
        return epgHotWordMapper.selectPage(new Page<>(epgHotWordBO.getPageNo(), epgHotWordBO.getPageSize()),wrapper);
    }

    private Wrapper<EpgHotWordDTO> getWrapperByBOInfo(EpgHotWordBO epgHotWordBO) {
        Wrapper<EpgHotWordDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("status", epgHotWordBO.getStatus());
        wrapper.eq("is_publish", epgHotWordBO.getIsPublish());
        //热词默认采用倒序
        wrapper.orderBy("search_num", false);
        return wrapper;
    }
}
